<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item prop="province">
        <el-select
          v-model="queryParams.provinceId"
          placeholder="全部省份"
          clearable
        >
          <el-option
            v-for="item in provinces"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="years">
        <el-select
          v-model="queryParams.years"
          placeholder="全部年份"
          clearable
        >
          <el-option
            v-for="item in years"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="cardType">
        <el-select
          v-model="queryParams.cardType"
          placeholder="全部类型"
          clearable
        >
          <el-option
            v-for="item in cardType"
            :key="item.id"
            :label="item.cardType"
            :value="item.cardType"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="disable">
        <el-select
          v-model="queryParams.disable"
          placeholder="是否禁用"
          clearable
        >
          <el-option
            v-for="item in disable"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="sold">
        <el-select
          v-model="queryParams.sold"
          placeholder="是否售出"
          clearable
        >
          <el-option
            v-for="item in sold"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="cardRefund">
        <el-select
          v-model="queryParams.cardRefund"
          placeholder="是否已退卡"
          clearable
        >
          <el-option
            v-for="item in cardRefund"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item prop="cardNum">
        <el-input
          v-model="queryParams.cardNum"
          placeholder="卡号/手机号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="expertName">
        <el-input
          v-model="queryParams.expertName"
          placeholder="高报师姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table ref="table" v-loading="loading" :data="typeList"
              @selection-change="handleSelectionChange"
              style="margin-top: 10px"
              :reserve-selection="true"
    >
      <el-table-column  width="55" align="center" fixed="left">
        <template slot="header">
          <el-checkbox
            :indeterminate="isIndeterminate"
            :value="isAllSelected"
            @change="handleSelectAll"
          ></el-checkbox>
        </template>
        <template slot-scope="scope">
          <el-checkbox
            :value="scope.row.selected"
            :disabled="scope.row.disabled === 1 || scope.row.returnCard === 1"
            @change="handleCheckboxChange(scope.row, $event)"
          ></el-checkbox>
        </template>
      </el-table-column>
      <el-table-column label="卡号" align="center" prop="cardNumber" min-width="150"/>
      <el-table-column label="卡用途" align="center" prop="cardUsage" min-width="120"/>
      <el-table-column label="售出方式" align="center" prop="saleMethod" min-width="120">
        <template slot-scope="scope">
          <span v-if="scope.row.saleMethod === '1'">线下</span>
          <span v-else-if="scope.row.saleMethod === '2'">线上</span>
          <span v-else-if="scope.row.saleMethod === '3'">非售卖</span>
        </template>
      </el-table-column>
      <el-table-column label="选科" align="center" prop="subjects"
                       min-width="140"
      >
        <template slot-scope="scope">
          {{ scope.row.subjects }}
        </template>
      </el-table-column>
      <el-table-column label="提分" align="center" prop="scoringSubjects" min-width="220">
        <template slot-scope="scope">
            <span v-if="scope.row.scoringSubjects">
            {{ scope.row.scoringSubjects }}-{{
                scope.row.numberOfSubjects
              }}科- {{
                scope.row.effectiveTime == null ? '有效期' + scope.row.expirationDate + "天" : '过期时间' + scope.row.effectiveTime
              }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="志愿" align="center" prop="distinguish" min-width="150">
        <template slot-scope="scope">
            <span v-if="scope.row.scoringSubjects">
            志愿卡|{{
                scope.row.effectiveTime == null ? '有效期' + scope.row.expirationDate + "天" : '过期时间' + scope.row.effectiveTime
              }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="省份" align="center" prop="province" min-width="100"/>
      <el-table-column label="禁用" align="center" prop="disabled" min-width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.disabled === 1 ? 'danger' : 'primary'"
                  disable-transitions>{{ scope.row.disabled === 1 ? '禁用' : '未禁用' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="退卡" align="returnCard" prop="disabled" min-width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.returnCard === 1 ? 'danger' : 'primary'"
                  disable-transitions>{{ scope.row.returnCard === 1 ? '退卡' : '未退卡' }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="姓名" align="center" prop="name" min-width="100"/>
      <el-table-column label="电话" align="center" prop="phone" min-width="120"/>
      <el-table-column label="学校" align="center" prop="school" min-width="100"/>
      <el-table-column label="高考年" align="center" prop="gaokaoYear" min-width="100"/>
      <el-table-column label="生产人" align="center" prop="producer" min-width="180"/>
      <el-table-column label="售出" align="center" prop="sale" min-width="100">
        <template slot-scope="scope">
          <el-tag :type="scope.row.sale === 1 ? 'primary' : 'success'"
                  disable-transitions>
            {{ scope.row.sale === 1 ? '售出' : '未售出' }}
          </el-tag>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {getCardManagement, getCardType, getUserProvince} from "@/api/expert/type";
  export default {
    name: "producedCards",
    data(){
      return{

        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,

        // 总条数
        total: 0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          cardNum: null,
          cardRefund: null,
          expertName: null,
          sold: null,
          disable: null,
          cardType: null,
          years: null,
          provinceId: null,
          //多账户查询
          cardNumbers: null,
        },

        // 卡管理表格数据
        typeList: [],
        //省份
        provinces: [],
        province: null,
        //卡类型
        cardType: [],
        cardRefund: [
          {value: '1', label: '已退卡'},
          {value: '0', label: '未退卡'},
        ],
        sold: [
          {value: '1', label: '已售出'},
          {value: '0', label: '未售出'},
        ],
        disable: [
          {value: '1', label: '已禁用'},
          {value: '0', label: '未禁用'},
        ],
        years: {},
      }
    },
    created() {
      this.getYears();
      this.getCardType();
      this.getUserProvince();
      this.getList();
    },
    //检测ids
    computed: {
      isAllSelected() {
        const selectableRows = this.typeList.filter(row => row.disabled !== 1 && row.returnCard !== 1);
        return selectableRows.length > 0 && selectableRows.every(row => row.selected);
      },
      isIndeterminate() {
        const selectableRows = this.typeList.filter(row => row.disabled !== 1 && row.returnCard !== 1);
        const selectedCount = selectableRows.filter(row => row.selected).length;
        return selectedCount > 0 && selectedCount < selectableRows.length;
      },
    },
    methods: {
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      //获取从今年开始五年的年份
      getYears() {
        const currentYear = new Date().getFullYear();
        this.years = [];
        for (let i = -2; i <= 3; i++) {
          const year = currentYear + i;
          this.years.push({
            value: year,
            label: year.toString()
          });
        }
      },

      //查询卡类型
      getCardType() {
        getCardType().then(response => {
          this.cardType = response.data;
        });
      },
      //查询省份
      getUserProvince() {
        getUserProvince().then(response => {
          if (Array.isArray(response.data)) {
            response.data.forEach(item => {
              this.provinces.push({
                value: item.id,
                label: item.province
              });
            });
          } else {
            // 如果 response.data 不是数组，直接添加
            this.provinces.push({
              value: response.data.id,
              label: response.data.province
            });
          }
        });
      },
      getList() {
        this.loading = true;
        //清空ids: []
        this.ids = [];
        console.log(this.queryParams)
        getCardManagement(this.queryParams).then(response => {
          if (response.code == 200) {
            this.typeList = response.data.list.map(item => ({
              ...item,
              selected: false
            }));
            this.selectedRows = [];
            this.total = response.data.total;
            this.loading = false;
          }
        });
      },
      handleCheckboxChange(row, checked) {
        // 更新 row 的 selected 属性
        // this.$set(row, 'selected', checked);
        // if (checked) {
        //     this.selectedRows.push(row);
        //     this.$set(row, 'selected', true);
        // } else {
        //     this.selectedRows.splice(index, 1);
        //     this.$set(row, 'selected', false);
        // }
        this.$set(row, 'selected', checked);

        if (checked) {
          this.selectedRows.push(row);
        } else {
          const index = this.selectedRows.findIndex(selectedRow => selectedRow.id === row.id);
          if (index !== -1) {
            this.selectedRows.splice(index, 1);
          }
        }
        // 更新 ids
        this.updateIds();

        // 更新 single 和 multiple
        this.single = this.selectedRows.length !== 1;
        this.multiple = this.selectedRows.length < 1;

        // 触发外部事件
        this.$emit('selection-change', this.selectedRows);
      },
      handleSelectionChange(selection) {
        this.selectedRows = selection;
        // 更新 ids
        this.updateIds();

        // 更新 single 和 multiple
        this.single = selection.length !== 1;
        this.multiple = selection.length < 1;
        // 触发外部事件
        this.$emit('selection-change', this.selectedRows);
      },
      handleSelectAll(checked) {
        this.selectedRows = [];
        this.typeList.forEach(row => {
          if (row.disabled !== 1 && row.returnCard !== 1) {
            this.$set(row, 'selected', checked);
            if (checked) {
              this.selectedRows.push(row);
            }
          }
        });
      //
        // 更新 ids
        this.updateIds();

        // 更新 single 和 multiple
        this.single = this.selectedRows.length !== 1;
        this.multiple = this.selectedRows.length < 1;

        // 触发外部事件
        this.$emit('selection-change', this.selectedRows);
      },
      updateIds() {
        this.ids = this.selectedRows.map(row => row.cardNumber);
        // this.cardUsageId =  this.selectedRows
        //   .filter(row => !row.cardUsage.includes('正式卡'))
        //   .map(row => row.cardUsageId);

      },
    }
  }
</script>

<style scoped lang="scss">

</style>
